package com.cx.exception;


import com.cx.util.ResultUtil;
import com.cx.util.WebUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@Component
// 鉴权失败处理器
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    private static final ObjectMapper MAPPER = new ObjectMapper();

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        ResultUtil result = new ResultUtil(HttpStatus.FORBIDDEN.value(), "您的权限不足！", null);
        String json = MAPPER.writeValueAsString(result);
        WebUtil.renderString(response, json);
    }
}
